﻿using System;
using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Collections;
using System.Windows.Controls;
using System.Windows;
using MengXunAppTool.Helper;

namespace MengXunAppTool.BLL
{
    public class FtpBLL
    {
        private string path = "ftp://cquni.com//";    //目标路径
        private string username = "yfgc";   //ftp用户名
        private string password = "yfgc";   //ftp密码
        private FtpHelper ftp = new FtpHelper("192.168.1.3", "yfgc", "yfgc");
        public List<string> GetDirList(string dir = "")
        {
            try
            {
                ftp.Disconnect();
                List<string> result = new List<string>();
                string[] dirc = dir.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in dirc)
                {
                    ftp.ChangeDir(item);
                }
                ArrayList dirlist = ftp.ListDirectories();
                foreach (string item in dirlist)
                {
                    string[] name = item.Split(' ');
                    if (name[name.Length - 1].Equals(".") || name[name.Length - 1].Equals(".."))
                    {
                        continue;
                    }
                    result.Add(name[name.Length - 1]);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public bool FileUpLoad(List<string> files, string ftpFolder, Sql3Helper.MX_AppVerRecord mX_VerRecord,UIBLL uIBLL,bool iscode)
        {
            try
            {
                if (iscode ==false)
                {
                    ftp.Disconnect();
                    MakeDir(ftpFolder, mX_VerRecord, uIBLL);
                    return ftp.OpenUpload(files[0], uIBLL,iscode);
                }
                else
                {
                    ftp.Disconnect();
                    MakeDir(ftpFolder, mX_VerRecord, uIBLL);
                    ftp.OpenUpload(files[0], uIBLL, iscode);
                    return ftp.OpenUpload(files[1], uIBLL, iscode);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 新建目录 上一级必须先存在
        /// </summary>
        /// <param name="dirName">服务器下的相对路径</param>
        public void MakeDir(string path, Sql3Helper.MX_AppVerRecord mX_VerRecord, UIBLL uIBLL)
        {
            try
            {
                ftp.Disconnect();
                string[] dirc = path.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in dirc)
                {
                    ftp.ChangeDir(item);
                }
                uIBLL.AddBarValue(5);
                ftp.MakeDir(mX_VerRecord.Customer);
                ftp.ChangeDir(mX_VerRecord.Customer);
                uIBLL.AddBarValue(5);
                ftp.MakeDir(mX_VerRecord.Product);
                ftp.ChangeDir(mX_VerRecord.Product);
                uIBLL.AddBarValue(5);
                ftp.MakeDir(mX_VerRecord.AppName);
                ftp.ChangeDir(mX_VerRecord.AppName);
                uIBLL.AddBarValue(5);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void MakeDir(string path, string name)
        {
            try
            {
                ftp.Disconnect();
                string[] dirc = path.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in dirc)
                {
                    ftp.ChangeDir(item);
                }
                ftp.MakeDir(name);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //从ftp服务器上下载文件的功能  
        public string Download(string fileName, string localPath)
        {
            try
            {
                FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + fileName));
                req.Method = WebRequestMethods.Ftp.GetFileSize;
                req.UseBinary = true;
                req.Credentials = new NetworkCredential(username, password);
                req.UsePassive = false;
                FtpWebResponse res = (FtpWebResponse)req.GetResponse();
                long cl = res.ContentLength;
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path + fileName));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(username, password);
                reqFTP.UsePassive = false;
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long lenth = 0;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                if (!Directory.Exists(localPath))
                {
                    Directory.CreateDirectory(localPath);
                }
                FileStream outputStream = new FileStream(localPath + fileName.Substring(fileName.LastIndexOf('\\')), FileMode.Create);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    lenth += readCount;
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
                return outputStream.Name;
            }
            catch (Exception ex)
            {
                return "";
                throw ex;
            }
        }
    }
}


